package com.finstone.data.spider.utils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;

import org.apache.commons.codec.binary.Base64;

public class ImageBase64Utils {
	public static String getImageStr(byte[] data) {
		return Base64.encodeBase64String(data);
	}

	public static boolean generateImage(String path, String filename, String imgStr) {
		if (imgStr == null) {
			return false;
		}

		try {

			byte[] b = Base64.decodeBase64(imgStr);
			for (int i = 0; i < b.length; ++i) {
				if (b[i] < 0) {
					b[i] += 256;
				}
			}

			File file = new File(path);
			String imgFilePath = path + filename;
			OutputStream out = new FileOutputStream(imgFilePath);
			out.write(b);
			out.flush();
			out.close();
			return true;
		} catch (Exception e) {
			System.out.println("e:" + e.getMessage());
			return false;
		}
	}
}
